package com.tepth.remote.control.delegate.apply.history;

import com.tepth.remote.control.base.bean.HistoryBean;
import com.tepth.remote.ui.recycler.BaseDataConverter;
import com.tepth.remote.ui.recycler.ItemType;
import com.tepth.remote.ui.recycler.MultipleFields;
import com.tepth.remote.ui.recycler.MultipleItemEntity;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/22
 */
public class ApplyHistoryDataConvert extends BaseDataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final ArrayList<SoapObject> soapArray = getSoapArray();
        final int size = soapArray.size();
        for (int i = 0; i < size; i++) {
            final SoapObject soapObject = soapArray.get(i);
            HistoryBean bean = new HistoryBean()
                    .setId(Integer.parseInt(soapObject.getPropertySafelyAsString("Id")))
                    .setSendUid(Integer.parseInt(soapObject.getPropertySafelyAsString("SendUid")))
                    .setRecUid(Integer.parseInt(soapObject.getPropertySafelyAsString("RecUid")))
                    .setContext(soapObject.getPropertySafelyAsString("MsgContext"))
                    .setTargetId(soapObject.getPropertySafelyAsString("TargetId"))
                    .setType(Integer.parseInt(soapObject.getPropertySafelyAsString("MsgType")))
                    .setCreateDate(soapObject.getPropertySafelyAsString("CreateDate"));
            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setItemType(ItemType.HISTORY_LIST)
                    .setField(MultipleFields.TITLE, bean.getContext())
                    .setField(MultipleFields.TEXT, bean.getCreateDate())
                    .setField(MultipleFields.ID, bean.getId())
                    .setField(MultipleFields.HISTORY_BEAN, bean)
                    .builder();
            ENTITIES.add(entity);
        }
        return ENTITIES;
    }

    private ArrayList<SoapObject> getSoapArray() {
        final ArrayList<SoapObject> soapArray = new ArrayList<>();
        final ArrayList<Object> array = getArrays();
        final int size = array.size();
        for (int i = 0; i < size; i++) {
            soapArray.add((SoapObject) array.get(i));
        }
        return soapArray;
    }
}
